import os
import cv2

#下采样每个4096*4096的图片至1024*·1024
def downsample_image(image_path, output_path, scale=0.25):
    # 读取原始图片
    image = cv2.imread(image_path)
    if image is None:
        raise FileNotFoundError(f"无法打开图像文件：{image_path}")

    # 下采样图片
    downsampled_image = cv2.resize(image, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR)

    # 保存下采样后的图片
    cv2.imwrite(output_path, downsampled_image)


def batch_downsample(input_dir, output_dir, scale=0.25):
    os.makedirs(output_dir, exist_ok=True)
    image_files = [f for f in os.listdir(input_dir) if f.endswith('.png')]

    for image_file in image_files:
        input_path = os.path.join(input_dir, image_file)
        output_path = os.path.join(output_dir, image_file)

        try:
            downsample_image(input_path, output_path, scale)
            print(f"Processed {image_file} and saved to {output_path}")
        except Exception as e:
            print(f"Error processing {image_file}: {e}")


# 示例用法
input_dir = 'E:\\wafer52\\11867'  # 替换为实际输入目录路径
output_dir = 'E:\\wafer52\\11867_32nm'  # 替换为实际输出目录路径

batch_downsample(input_dir, output_dir, scale=0.25)

print("Batch downsampling completed.")
